//地址编辑
<template>
  <div>
    <van-nav-bar
      title="新增地址"
      placeholder
      fixed
      left-arrow
      @click-left="goBack"
    />
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      :address-info="{
        name: info.name,
        tel: info.tel,
        addressDetail: info.addressDetail,
        areaCode: info.areaCode,
        postalCode: info.postalCode,
        isDefault: info.isDefault,
      }"
    />
  </div>
</template>
<script>
import { areaList } from "@vant/area-data";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      info: [],
      uname: sessionStorage.getItem("uname"),
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    onSave(content) {
      this.isDefault = content.isDefault ? "1" : "0"; //数据库该项设置为0，1，如不需修改可删除该行
      //如果地址id不存在，则连接新增地址接口，存在则连接修改地址接口
      if (!this.$route.query.id) {
        //发送请求连接新增地址接口
        this.axios
          .post(
            "/address/addAddress",
            `uname=${this.uname}&name=${content.name}&phone=${content.tel}&province=${content.province}&city=${content.city}&county=${content.county}&detail_addr=${content.addressDetail}&group_message=${this.isDefault}&areaCode=${content.areaCode}&postalCode=${content.postalCode}`
          )
          .then((res) => {
            console.log(res);
          });
      } else {
        //发送请求连接修改地址接口
        this.axios
          .post(
            "/addressupdateAddress",
            `id=${this.$route.query.id}&name=${content.name}&phone=${content.tel}&province=${content.province}&city=${content.city}&county=${content.county}&detail_addr=${content.addressDetail}&group_message=${this.group_message}&areaCode=${content.areaCode}&postalCode=${content.postalCode}`
          )
          .then((res) => {
            console.log(res);
          });
      }
      this.$toast.success("保存成功");
      //返回地址列表页，强制刷新
      this.$router.replace("/addresslist");
      // location.reload();
    },
    onDelete() {
      let id = this.$route.query.id;
      const url = "/address/delAddress";
      const params = `id=${id}`;
      //发送请求连接删除地址接口
      this.axios.post(url, params).then((res) => {
        console.log(res.data);
      });
      this.$toast.success("删除成功");
      this.$router.go(-1);
    },
  },
  mounted() {
    //获取地址列表传的地址id
    let id = this.$route.query.id;
    const url = "/address/addressId";
    const params = `id=${id}`;
    //地址id存在则发请求回显地址
    if (id) {
      //发送请求获取地址内容
      this.axios.post(url, params).then((res) => {
        //通过address-info回显地址
        this.info = res.data.results;
        this.info.isDefault = this.info.isDefault == 1 ? true : false;
        console.log(this.info.isDefault);
        console.log(this.info);
      });
    }
  },
};
</script>
